<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class FileListForm extends CFormModel implements IMiniyunGridConfig
{
    public $searchKey;    public $renameKey;    public $renameID;    public $action;    public $revertAction;
    public $revertActionArray = array(""=>"批量操作","revert"=>"还原","delete"=>"彻底删除");
    public $removeAction;
    public $removeActionArray = array(""=>"批量操作","remove"=>"删除到回收站","delete"=>"彻底删除");
    public $revertAllAction;
    public $deleteAllAction;
    public $removeAllAction;

    public function rules()
    {
        return array(
        array('searchKey,renameKey,renameID,action,revertAction,removeAction,revertAllAction,removeAllAction,deleteAllAction', 'length','max'=>128),
        );
        return $valid;
    }

    public function init(){
        parent::init();
        $this->revertAction="";
        $this->removeAction="";
        $this->removeAllAction="";
        $this->revertAllAction="";
        $this->deleteAllAction="";
    }
    
    public function getFormFilters(){
    	$officeConditons = "";
    	$officeTypes = Yii::app()->params['officeType'];
    	$officeConditons = "'".implode("','",array_values($officeTypes))."'"; 
        return array(
        	'office'=>array(
        		"condition"=>"select id from ".Yii::app()->params['tablePrefix']."files where version_id in(select id from ".Yii::app()->params['tablePrefix']."file_versions where mime_type in($officeConditons))"
        ),
            'image'=>array(
        		"condition"=>"select id from ".Yii::app()->params['tablePrefix']."files where version_id in(select id from ".Yii::app()->params['tablePrefix']."file_versions where mime_type like 'image/%')"
        ),
        	'trash'=>array(
        		"condition"=>"select id from ".Yii::app()->params['tablePrefix']."files where is_deleted=1"
        ),
            'author'=>array(
        		"condition"=>"select id from ".Yii::app()->params['tablePrefix']."files where user_id=?"
        ),
            'directory'=>array(
        	"condition"=>"select id from ".Yii::app()->params['tablePrefix']."files where parent_file_id=?"
        ),
        );
    }
    
    public function getSearchKey(){
        if($this->action=="search"){
                        $key = $this->searchKey;
            return "select id from ".Yii::app()->params['tablePrefix']."files where file_name like '%$key%'";
        }
    }
    
    public function _getMultiNextActions(){

        $arr = array('action' => Yii::app()->getController()->mAction);
        return array(
            'removeAction'=>array(                "remove"=>Yii::app()->createUrl("adminFilePanel/remove", $arr),
                "delete"=>Yii::app()->createUrl("adminFilePanel/delete", $arr),
        ),
            'revertAction'=>array(                "revert"=>Yii::app()->createUrl("adminFilePanel/revert", $arr),
                "delete"=>Yii::app()->createUrl("adminFilePanel/delete", $arr),

        ),
            'removeAll' => Yii::app()->createUrl("adminFilePanel/removeAll", $arr),
            'revertAll' => Yii::app()->createUrl("adminFilePanel/revertAll", $arr),
            'deleteAll' => Yii::app()->createUrl("adminFilePanel/deleteAll", $arr),
        );
    }

    
    public function getDataProvider()
    {
        return new FileListDataProvider($this);
    }
    
    public function formAction()
    {
        if(empty($this->action))
        {
            return false;
        }

                if ($this->_mutiAction())
        {
            return true;
        }
        if ($this->_updateAction())
        {
            return true;
        }
        return false;
    }
    
    private function _mutiAction(){
        $list = $this->_getMultiNextActions();
        if($this->action=="removeAction")
        {
                        if(isset($this->removeAction) && strlen($this->removeAction)>0){
                $url = $list['removeAction'][$this->removeAction];
            }
        }  else if($this->action=="revertAction")
        {
                        if(isset($this->revertAction) && strlen($this->revertAction)>0){
                $url = $list['revertAction'][$this->revertAction];
            }
        }
        if(isset($url)){
            $ids = Yii::app()->request->getParam("selectItems");
            if($ids!='' && $url!=''){
                                $url = $url."&id=".implode(",", array_values($ids));;
                Yii::app()->getController()->redirect($url);
                return true;
            }
        }
        return false;
    }

    
    private function _updateAction()
    {
                                $mAction = Yii::app()->getController()->mAction;
                                if ($this->action === 'removeAllAction')
        {
            Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/removeAll"));
            return true;
        }
                                if ($this->action === 'revertAllAction')
        {
            Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/revertAll"));
            return true;
        }
                                if ($this->action === 'deleteAllAction')
        {
            Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/deleteAll"));
            return true;
        }
                                if ($this->action === 'renameAction')
        {
            Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/rename",
            $this->getParamers(array('action'=>$mAction, 'id'=>$this->renameID, 'name'=>$this->renameKey))));
            return true;
        }
                                if ($this->action === 'cancelAction')
        {
            $id = Yii::app()->request->getParam("id");
            if ($mAction === 'show')
            {
                Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/".$mAction, array('id'=>$this->renameID)));
                return true;
            }


            Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/".$mAction, $this->getParamers()));
            return true;
        }
        return false;
    }
    
    public function getParamers($arr=array())
    {
                                $pageArr = array();
        $page = Yii::app()->request->getParam('page');
        if (isset($page))
        {
            $pageArr['page'] = $page;
        }

        return array_merge($pageArr,$arr);
    }
}